<?php

namespace App\Supports\Payment\Methods\Goldcoin;

use Carbon\Carbon;
use App\Models\GoldRecord;
use Illuminate\Support\Facades\DB;
use App\Events\GoldRecordPaidEvent;
use App\Supports\Payment\Methods\Method;
use App\Supports\Payment\Contracts\MethodPay;

class PayStorm extends Method implements MethodPay
{
	public function validate($order = null) {}

	public function info($order = null) {}

	public function pay($order = null)
	{
        /**
         * @var GoldRecord $order
         */
		return DB::transaction(function () use ($order) {

			$coins = $order->pay_net_price;
            $cashes = $order->pay_cash_price;

            $data = ['gold_record_id' => $order->id];
            account_coins($order->user)->minusStormPaid($coins, $data);
            account_cashes($order->user)->minusStormPaid($cashes, $data);

            $order->update(['paid_at' => Carbon::now(), 'paid_method' => 'goldcoin']);

			event(new GoldRecordPaidEvent($order));

			$order->unsetRelations();

			return $order;
		});
	}
}
